package com.liyuxiang.yygh.hosp.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.liyuxiang.yygh.common.helper.HttpRequestHelper;
import com.liyuxiang.yygh.common.result.Result;
import com.liyuxiang.yygh.common.utils.MD5;
import com.liyuxiang.yygh.hosp.service.DepartmentService;
import com.liyuxiang.yygh.hosp.service.HospitalService;
import com.liyuxiang.yygh.hosp.service.HospitalSetService;
import com.liyuxiang.yygh.hosp.service.ScheduleService;
import com.liyuxiang.yygh.model.hosp.Department;
import com.liyuxiang.yygh.model.hosp.Hospital;
import com.liyuxiang.yygh.model.hosp.Schedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    HospitalService hospitalService;
    @Autowired
    HospitalSetService hospitalSetService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    ScheduleService scheduleService;

    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hospSign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        if (hospSign.equals(MD5.encrypt(signKey))) {
            paramMap.put("logoData", ((String) paramMap.get("logoData")).replaceAll(" ", "+"));
            hospitalService.saveHospital(paramMap);
            return Result.ok();
        }
        return Result.fail().message("签名校验失败");
    }

    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hospSign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        if (hospSign.equals(MD5.encrypt(signKey))) {
            Hospital hospital = hospitalService.getHospitalByHoscode(hoscode);
            return Result.ok(hospital);
        }
        return Result.fail().message("签名校验失败");
    }


    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hospSign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        if (hospSign.equals(MD5.encrypt(signKey))) {
            departmentService.saveDepartment(paramMap);
            return Result.ok();
        }
        return Result.fail().message("签名校验失败");
    }

    @PostMapping("/department/list")
    public Result getDepartment(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hospSign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        if (hospSign.equals(MD5.encrypt(signKey))) {
            int pageNum = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt(((String) paramMap.get("page")));
            int pageSize = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt(((String) paramMap.get("limit")));
            Page<Department> list = departmentService.getDepartment(hoscode, pageNum, pageSize);
            return Result.ok(list);
        }
        return Result.fail().message("签名校验失败");
    }
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hospSign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        if (hospSign.equals(MD5.encrypt(signKey))) {
            String depcode = (String) paramMap.get("depcode");
            departmentService.removeDepartment(depcode,hoscode);
            return Result.ok();
        }
        return Result.fail().message("签名校验失败");
    }

    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hospSign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        if (hospSign.equals(MD5.encrypt(signKey))) {
            scheduleService.saveSchedule(paramMap);
            return Result.ok();
        }
        return Result.fail().message("签名校验失败");
    }
    @PostMapping("/schedule/list")
    public Result getSchedule(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hospSign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        if (hospSign.equals(MD5.encrypt(signKey))) {
            int pageNum = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt(((String) paramMap.get("page")));
            int pageSize = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt(((String) paramMap.get("limit")));
            Page<Schedule> list = scheduleService.getSchedule(hoscode, pageNum, pageSize);
            return Result.ok(list);
        }
        return Result.fail().message("签名校验失败");
    }
    @PostMapping("/schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hospSign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        if (hospSign.equals(MD5.encrypt(signKey))) {
            String hosScheduleId = (String) paramMap.get("hosScheduleId");
            scheduleService.removeSchedule(hoscode,hosScheduleId);
            return Result.ok();
        }
        return Result.fail().message("签名校验失败");
    }
}
